//1.引入Vue
import Vue from 'vue';
import Vuex from 'vuex';
import view from '@/router/baseView';
import router from '@/router/index'
import dynamicView from "@/router/dynamicView";
//2.安装
Vue.use(Vuex);
const store = new Vuex.Store({
    //存储状态
    state:{
        userNav: [],    // 导航栏路由, 默认未登录，展示静态路由
        activeIndex: '/',
        tags: [
            { name: '首页', path: '/index' }
        ],
        tagMap: new Map(),
    },
    // Vuex中的Store状态（state）更新的唯一方式：提交Mutation
    mutations:{//定义mutations
        // 定义改变全局共享数据的方法
        setMenu(state) {
            let baseView = Object.assign([], view) // 初始化导航栏
            // 根据菜单权限获取菜单
            let nav = getNav(dynamicView)
            nav.forEach(item => {
                baseView.push(item)
            })
            state.userNav = baseView
            // 设置 router
            let list = setRouters(nav)
            list.forEach(o => {
                state.tagMap.set(o.path, o.name)
            })
        },
        navClick(state, path) {
            let pathList = []
            state.tags.forEach(tag => {
                pathList.push(tag.path)
            })
            if (pathList.indexOf(path) === -1 && path !== '/') {
                let tag = {
                    name: state.tagMap.get(path),
                    path: path
                }
                state.tags.push(tag)
            }
        },
        closeTagOp(state, tag) {
            let index = state.tags.indexOf(tag)
            state.tags.splice(index, 1)
        },
        closeOthersTagsOp(state, tag) {
            let obj = state.tags[0]
            state.tags = [obj, tag]
        },
        closeAllTagsOp(state) {
            let obj = state.tags[0]
            state.tags = [obj]
        },
        closeLeftTagsOp(state, tag) {
            let index = state.tags.indexOf(tag)
            let obj = state.tags[0]
            state.tags = state.tags.slice(index)
            state.tags.unshift(obj)
        },
        closeRightTagsOp(state, tag) {
            let index = state.tags.indexOf(tag)
            state.tags = state.tags.slice(0, index + 1)
        }
    },
    actions:{

    },
    getters:{

    }
})
//导出
export default store

function getNav(navs) {
    let list = []
    navs.forEach(nav => {
        let obj = {}
        obj.path = nav.path
        obj.name = nav.name
        obj.icon = nav.icon
        obj.isPage = nav.isPage
        if (nav.isPage) {
            obj.component = () => import ('@/views' + nav.path)
        }
        if (nav.children) {
            obj.children = getNav(nav.children)
        }
        list.push(obj)
    })
    return list
}
function setRouters(navs) {
    let list = []
    getRouter(list, navs)
    let routerObj = {
        path: '/',
        hidden: true,
        component: () =>
            import ('@/page/layout'),
        children: list
    }
    router.addRoute(routerObj)
    return list
}

function getRouter(list, navs) {
    navs.forEach(nav => {
        if (nav.isPage) {
            list.push(nav)
        } else {
            if (nav.children) {
                getRouter(list, nav.children)
            }
        }
    })
}
